/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BookTest.java
 *
 *
 */
package UI;



import Model.Book;
import Model.Category;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import java.util.ArrayList;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class frmBook extends javax.swing.JDialog {

    DefaultTableModel model;
    Book book;
    ArrayList<Book> arrBook;
    ArrayList <Book> arrAddBook;
    int rowSelected=-1;
    int rowAddSelect =-1;
    Socket cs;
    ObjectOutputStream out;
    ObjectInputStream in;
    /** Creates new form BookTest */
    public frmBook(java.awt.Frame parent, boolean modal,ObjectOutputStream out,ObjectInputStream in )  {
             super(parent, modal);
        //ObjectOutputStream out,   ObjectInputStream in
        
            initComponents();
            model = (DefaultTableModel) tbUpdate.getModel();
            book = new Book();
            arrBook = new ArrayList<>();
            arrAddBook= new ArrayList<>();
            this.in = in; this.out = out;
             
            //mo luong out,in de goi nhan du lieu voi server
           
            //load category
            loadDataCategory();
        
         
        
    }
    private void loadDataCategory() {
        try {
         
           ArrayList<String> arrString=new ArrayList<>();
           out.writeObject("getCategoryName");
           arrString = (ArrayList)in.readObject();
           String[] s = new String[arrString.size()];
           arrString.toArray(s);        
           DefaultComboBoxModel model=new DefaultComboBoxModel(s);
           cboCategory.setModel(model);
        } catch (ClassNotFoundException | IOException ex) {
            ex.printStackTrace();
        }
    }
  //************ method exit form**************
    public void cancel() {
        int option = JOptionPane.showConfirmDialog(this, "Are you sure want to exit ?","Exit",JOptionPane.OK_CANCEL_OPTION);     
        if (option == JOptionPane.OK_OPTION) {
            this.setVisible(false);
            this.dispose();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        pnMain = new javax.swing.JPanel();
        lblpicture = new javax.swing.JLabel();
        tabChangeBook = new javax.swing.JTabbedPane();
        pnChangeBook = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        btnUpdate1 = new javax.swing.JButton();
        btnDelete1 = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbUpdate = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        txtSearchUpdate = new javax.swing.JTextField();
        cbSearchUpdate = new javax.swing.JComboBox();
        btnSearchUpdate = new javax.swing.JButton();
        pnAddBook = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbAddBook = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        lblBookID = new javax.swing.JLabel();
        txtBookID = new javax.swing.JTextField();
        lblTitle = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        lblAuthor = new javax.swing.JLabel();
        txtAuthor = new javax.swing.JTextField();
        lblCategory = new javax.swing.JLabel();
        cboCategory = new javax.swing.JComboBox();
        lblPublisher = new javax.swing.JLabel();
        btnNewCategory = new javax.swing.JButton();
        btnEditCategory = new javax.swing.JButton();
        txtPublisher = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtQuantity = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtSumary = new javax.swing.JTextArea();
        lbSumaryAdd = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnInsert = new javax.swing.JButton();
        btnChange = new javax.swing.JButton();
        btnComplete = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnCancel1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Books Management System");
        setResizable(false);

        pnMain.setBackground(new java.awt.Color(0, 215, 255));
        pnMain.setPreferredSize(new java.awt.Dimension(800, 600));

        lblpicture.setBackground(new java.awt.Color(255, 0, 0));
        lblpicture.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Books.png"))); // NOI18N

        tabChangeBook.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        tabChangeBook.setVerifyInputWhenFocusTarget(false);

        pnChangeBook.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Update And Delete", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        btnUpdate1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Modify.png"))); // NOI18N
        btnUpdate1.setText("Update");
        btnUpdate1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdate1ActionPerformed(evt);
            }
        });
        jPanel2.add(btnUpdate1);

        btnDelete1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Delete.png"))); // NOI18N
        btnDelete1.setText("Delete");
        btnDelete1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelete1ActionPerformed(evt);
            }
        });
        jPanel2.add(btnDelete1);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Exit.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jPanel2.add(btnCancel);

        tbUpdate.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Book ID", "Title ", "Author", "Category"
            }
        ));
        tbUpdate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbUpdateMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbUpdate);

        txtSearchUpdate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtSearchUpdateMouseClicked(evt);
            }
        });

        cbSearchUpdate.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-----Choose------", "Book ID", "Title", "Category", "Author" }));

        btnSearchUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonSearch.png"))); // NOI18N
        btnSearchUpdate.setText("Search");
        btnSearchUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchUpdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbSearchUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(txtSearchUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(51, 51, 51)
                .addComponent(btnSearchUpdate)
                .addContainerGap(273, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbSearchUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSearchUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearchUpdate))
                .addGap(30, 30, 30))
        );

        javax.swing.GroupLayout pnChangeBookLayout = new javax.swing.GroupLayout(pnChangeBook);
        pnChangeBook.setLayout(pnChangeBookLayout);
        pnChangeBookLayout.setHorizontalGroup(
            pnChangeBookLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnChangeBookLayout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 711, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnChangeBookLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 721, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pnChangeBookLayout.setVerticalGroup(
            pnChangeBookLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnChangeBookLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        tabChangeBook.addTab("View Book         ", new javax.swing.ImageIcon(getClass().getResource("/Images/UpdateBook.png")), pnChangeBook); // NOI18N

        pnAddBook.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add New Book", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        tbAddBook.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Book ID", "Title ", "Author", "Publisher", "Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbAddBook.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbAddBookMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbAddBook);

        lblBookID.setText("Book ID");

        lblTitle.setText("Title");

        txtTitle.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtTitleCaretUpdate(evt);
            }
        });

        lblAuthor.setText("Author");

        txtAuthor.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtAuthorCaretUpdate(evt);
            }
        });

        lblCategory.setText("Category");

        cboCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboCategoryActionPerformed(evt);
            }
        });

        lblPublisher.setText("Publisher");

        btnNewCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonNew16px.png"))); // NOI18N
        btnNewCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewCategoryActionPerformed(evt);
            }
        });

        btnEditCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/buttonEdit16px.png"))); // NOI18N
        btnEditCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditCategoryActionPerformed(evt);
            }
        });

        jLabel1.setText("Quantity");

        txtSumary.setColumns(20);
        txtSumary.setRows(5);
        jScrollPane3.setViewportView(txtSumary);

        lbSumaryAdd.setText("Sumary");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAuthor)
                    .addComponent(lblTitle)
                    .addComponent(lblBookID)
                    .addComponent(jLabel1))
                .addGap(40, 40, 40)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtQuantity, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                    .addComponent(txtBookID, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                    .addComponent(txtAuthor, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                    .addComponent(txtTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE))
                .addGap(102, 102, 102)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblCategory)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lbSumaryAdd)
                        .addComponent(lblPublisher)))
                .addGap(30, 30, 30)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtPublisher, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                            .addComponent(cboCategory, 0, 161, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnNewCategory)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEditCategory)
                        .addGap(10, 10, 10))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtBookID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblBookID)
                                    .addComponent(lblCategory)
                                    .addComponent(cboCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel5Layout.createSequentialGroup()
                                        .addGap(20, 20, 20)
                                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(lblTitle)
                                            .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(jPanel5Layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(lblPublisher)
                                            .addComponent(txtPublisher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addComponent(btnEditCategory)
                            .addComponent(btnNewCategory))
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addGap(24, 24, 24)
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(lblAuthor)
                                    .addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane3, 0, 0, Short.MAX_VALUE))))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addComponent(lbSumaryAdd)))
                .addContainerGap(69, Short.MAX_VALUE))
        );

        btnInsert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Add.png"))); // NOI18N
        btnInsert.setText("Insert");
        btnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsertActionPerformed(evt);
            }
        });
        jPanel1.add(btnInsert);

        btnChange.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonUpdate32px.png"))); // NOI18N
        btnChange.setText("Change");
        btnChange.setEnabled(false);
        btnChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeActionPerformed(evt);
            }
        });
        jPanel1.add(btnChange);

        btnComplete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonComplete.png"))); // NOI18N
        btnComplete.setText("Complete");
        btnComplete.setEnabled(false);
        btnComplete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCompleteActionPerformed(evt);
            }
        });
        jPanel1.add(btnComplete);

        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/reset.png"))); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        jPanel1.add(btnReset);

        btnCancel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Exit.png"))); // NOI18N
        btnCancel1.setText("Cancel");
        btnCancel1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancel1ActionPerformed(evt);
            }
        });
        jPanel1.add(btnCancel1);

        jLabel2.setText("All Information Of Title Book and Book :");

        javax.swing.GroupLayout pnAddBookLayout = new javax.swing.GroupLayout(pnAddBook);
        pnAddBook.setLayout(pnAddBookLayout);
        pnAddBookLayout.setHorizontalGroup(
            pnAddBookLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(pnAddBookLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnAddBookLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
                    .addGroup(pnAddBookLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 513, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(pnAddBookLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnAddBookLayout.setVerticalGroup(
            pnAddBookLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnAddBookLayout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        tabChangeBook.addTab("Add New Book", new javax.swing.ImageIcon(getClass().getResource("/Images/AddBook.png")), pnAddBook); // NOI18N

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/BannerDVT.gif"))); // NOI18N

        javax.swing.GroupLayout pnMainLayout = new javax.swing.GroupLayout(pnMain);
        pnMain.setLayout(pnMainLayout);
        pnMainLayout.setHorizontalGroup(
            pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnMainLayout.createSequentialGroup()
                .addComponent(lblpicture)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 672, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(91, Short.MAX_VALUE))
            .addComponent(tabChangeBook, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 897, Short.MAX_VALUE)
        );
        pnMainLayout.setVerticalGroup(
            pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnMainLayout.createSequentialGroup()
                .addGroup(pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnMainLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblpicture, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(tabChangeBook, javax.swing.GroupLayout.PREFERRED_SIZE, 532, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        tabChangeBook.getAccessibleContext().setAccessibleDescription("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnMain, javax.swing.GroupLayout.DEFAULT_SIZE, 897, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnMain, javax.swing.GroupLayout.DEFAULT_SIZE, 702, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbAddBookMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbAddBookMouseClicked
        rowAddSelect = tbAddBook.getSelectedRow();
        if (rowAddSelect < 0) {
            return;
        }
        formAdd(arrAddBook.get(rowAddSelect));

    }//GEN-LAST:event_tbAddBookMouseClicked
 
    private void formAdd(Book book) {
        txtBookID.setText(book.BookID);      
        txtTitle.setText(book.Title);
        txtAuthor.setText(book.Author);
        txtSumary.setText(book.Sumary);
        txtPublisher.setText(book.Publisher);
       
    }
    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
       
        resetFormAdd();
     
         DefaultTableModel modelTbAddBook =(DefaultTableModel) tbAddBook.getModel();
         while (tbAddBook.getRowCount() > 0) {
            modelTbAddBook.removeRow(tbAddBook.getRowCount() - 1);
        }
    }//GEN-LAST:event_btnResetActionPerformed
    
    public void resetFormAdd()
    {
        txtTitle.setText("");
        txtAuthor.setText("");
        txtPublisher.setText("");
        txtBookID.setText("");
        txtQuantity.setText("");
        txtQuantity.setText("");
        txtSumary.setText("");
    }
    private void tbUpdateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbUpdateMouseClicked
        // TODO add your handling code here:
         rowSelected = tbUpdate.getSelectedRow();
    }//GEN-LAST:event_tbUpdateMouseClicked

    private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed
        // TODO add your handling code here:
          int quantity =1;
       
        if (validateData()) {
            btnChange.setEnabled(true);
            btnComplete.setEnabled(true);
            if(!txtQuantity.getText().equals(""))
                   quantity = Integer.parseInt(txtQuantity.getText());
        //    String bookID = txtBookID.getText();     
            for (int i = 1; i <= quantity; i++) {
                Book b = new Book();
                b.BookID =  txtBookID.getText()+Integer.toString(i);
                b.Title = txtTitle.getText();
                b.Sumary = txtSumary.getText();
                b.Author = txtAuthor.getText();
                b.Publisher = txtPublisher.getText();
                b.CategoryID = Integer.toString(cboCategory.getSelectedIndex());
                b.setCategoryName(cboCategory.getSelectedItem().toString());
                //b.CategoryID = new Category().returnCategoryID((String) cboCategory.getSelectedItem());
               // b.BookID = bookID;  
                b.Active = true;
                arrAddBook.add(b);
            }
            loadTableAdd(arrAddBook,0);
            resetFormAdd();
            
            txtQuantity.setEnabled(false);
            cboCategory.setEnabled(false);

        }
    }//GEN-LAST:event_btnInsertActionPerformed
     private void loadTableAdd(ArrayList<Book> arr,int flag) {
       DefaultTableModel modelTbAddBook =(DefaultTableModel) tbAddBook.getModel();
        while (tbAddBook.getRowCount() > 0) {
            modelTbAddBook.removeRow(tbAddBook.getRowCount() - 1);
        }
        for (Book b : arr) {
            Vector vectorAdd = b.toVector();
            if(flag ==1)
                vectorAdd.addElement("Failed");
            else
                vectorAdd.addElement("On going");
            modelTbAddBook.addRow(vectorAdd);
           
        }

    }
     public boolean validateData() {
        if (txtBookID.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Book ID cannot blank");
            txtBookID.requestFocus();
            return false;
        }
        if (txtTitle.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Title cannot blank");
            txtTitle.requestFocus();
            return false;
        }
        if (txtAuthor.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Author cannot blank");
            txtAuthor.requestFocus();
            return false;
        }
        
        return true;
    }
    private void btnUpdate1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdate1ActionPerformed
//        UpdateBook updatebook = new UpdateBook(null,true);
//        updatebook.setLocationRelativeTo(this);
//        updatebook.setVisible(true);
       if(rowSelected==-1){
            JOptionPane.showMessageDialog(this,"Please choice data");
            return;
        }
        UpdateBook updatebook = new UpdateBook(null,true,arrBook.get(rowSelected),out,in);
      //  updatebook.setLocationRelativeTo(this);
        updatebook.setVisible(true);
        ShowData();
        rowSelected=-1;
    }//GEN-LAST:event_btnUpdate1ActionPerformed
      private void ShowData(){
        removeTableUpdate();
        if(!arrBook.isEmpty()){
            for(Book b:arrBook){
                model.addRow(b.toVector());
            }
        }
    }
    private void btnDelete1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelete1ActionPerformed
        int option = JOptionPane.showConfirmDialog(this, "Are you sure want to delete ?","Delete",JOptionPane.OK_CANCEL_OPTION);     
        if (option == JOptionPane.CANCEL_OPTION) {
            return;
        }      
        try {
           
            if(rowSelected==-1){
                return;
            }
            out.writeObject("deleteBook"); 
            Book b;
            b=arrBook.get(rowSelected);
            out.writeObject(b);//goi doi tuong sinh vien cho sever        
            boolean   alert=(boolean)in.readObject();//nhan thong bao tu server    
            if (alert) {
               // JOptionPane.showMessageDialog(this, "Delete Success");
                model.removeRow(rowSelected);
                arrBook.remove(b);            
            }        
            rowSelected=-1;
        }  catch (ClassNotFoundException| IOException ex) {
            System.out.println(ex);
        }  
       
    }//GEN-LAST:event_btnDelete1ActionPerformed

    private void cboCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboCategoryActionPerformed
        // TODO add your handling code here:
        
     
    }//GEN-LAST:event_cboCategoryActionPerformed
  
    private void txtTitleCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtTitleCaretUpdate
        // TODO add your handling code here:
     
    }//GEN-LAST:event_txtTitleCaretUpdate

    private void txtAuthorCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtAuthorCaretUpdate
        // TODO add your handling code here:      
      

    }//GEN-LAST:event_txtAuthorCaretUpdate
  private void removeTableUpdate() {
        while (tbUpdate.getRowCount() > 0) {
            model.removeRow(tbUpdate.getRowCount() - 1);
        }
    }
    
    private void ShowAllData(){
        String keyWord = txtSearchUpdate.getText();
        if (keyWord.equals("")) {
           try {
               out.writeObject("showAllBook");               
                removeTableUpdate();                        
                  arrBook=(ArrayList)in.readObject();
                  
                 for (int i = 0; i < arrBook.size(); i++) {
                   Book  b = (Book) arrBook.get(i);
                 model.addRow(b.toVector());
                }

            } catch (IOException|ClassNotFoundException|NullPointerException ex) {
                System.out.println(ex);
            } 
        }
    }
private void txtSearchUpdateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtSearchUpdateMouseClicked
// TODO add your handling code here:
     ShowAllData();
}//GEN-LAST:event_txtSearchUpdateMouseClicked

private void btnSearchUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchUpdateActionPerformed
        // TODO add your handling code here:
    //1 : bookID , 2. title , 3.Author, 4.category
        int index = cbSearchUpdate.getSelectedIndex();
        String keyWord = txtSearchUpdate.getText();
        if(index==1)
        {
            try {
                out.writeObject("searchBookID");
                out.writeObject(keyWord);
                out.writeObject(0);
                
                book = (Book)in.readObject();
                if (book == null) {
                JOptionPane.showMessageDialog(this, "No data found");
            } else {
                arrBook.removeAll(arrBook);
                arrBook.add(book); //bookId chi tim dc dung 1 quyen
                removeTableUpdate();
                model.addRow(book.toVector());
            }                    
            } catch (ClassNotFoundException | IOException ex) {      
                System.out.println(ex);
            }
            
        }
        else if (index == 2 || index == 3) {
              try {
                out.writeObject("searchTitleOrCategory");
                out.writeObject(keyWord+"#"+index+"#"+"0");
                arrBook=(ArrayList)in.readObject();
                 if (arrBook.isEmpty()) {
                JOptionPane.showMessageDialog(this, "No data found");
                } else {
                     removeTableUpdate();
                      for (Book b : arrBook) {
                             model.addRow(b.toVector());
                             }

                       }
                } catch (ClassNotFoundException | IOException ex) {      
                System.out.println(ex);
            }
        }
        else if(index==4)
        {
            try {
                out.writeObject("searchAuthor");
                out.writeObject(keyWord+"#"+"0");
                arrBook = (ArrayList)in.readObject();
                if (arrBook.isEmpty()) {
                JOptionPane.showMessageDialog(this, "No data found");
                 } else {
                removeTableUpdate();
                for (Book b : arrBook) {
                    model.addRow(b.toVector());
                              }
                         }
                
            } catch (ClassNotFoundException | IOException ex) {
                    System.out.println(ex);
            }
            
           
        }

}//GEN-LAST:event_btnSearchUpdateActionPerformed

private void btnCancel1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancel1ActionPerformed
// TODO add your handling code here:
    cancel();
}//GEN-LAST:event_btnCancel1ActionPerformed

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
// TODO add your handling code here:
    cancel();
}//GEN-LAST:event_btnCancelActionPerformed

private void btnNewCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewCategoryActionPerformed
        // TODO add your handling code here:
        dialogNewCategory category = new dialogNewCategory(null,true,out,in);
        category.setLocationRelativeTo(this);
        category.setVisible(true);
        loadDataCategory();
}//GEN-LAST:event_btnNewCategoryActionPerformed

private void btnEditCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditCategoryActionPerformed
        // TODO add your handling code here:
        Category oldCate = new Category(Integer.toString(cboCategory.getSelectedIndex()),(String)cboCategory.getSelectedItem());
        dialogEditCategory category = new dialogEditCategory(null,true,oldCate,out,in);
        category.setLocationRelativeTo(this);
        category.setVisible(true);
        loadDataCategory();
}//GEN-LAST:event_btnEditCategoryActionPerformed

private void btnChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeActionPerformed
        // TODO add your handling code here:
        Book b = new Book();
        Book  tempBook= arrAddBook.get(rowAddSelect);
        b.BookID = txtBookID.getText();
        b.Title = txtTitle.getText();
        b.Author = txtAuthor.getText();
        b.Publisher = txtPublisher.getText();
        b.Sumary = txtSumary.getText();
        b.CategoryID = tempBook.CategoryID;
        arrAddBook.set(rowAddSelect, b);
        loadTableAdd(arrAddBook,0);
       
        
}//GEN-LAST:event_btnChangeActionPerformed

private void btnCompleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCompleteActionPerformed
        try {
            // TODO add your handling code here:
           boolean resultAdd;
           ArrayList<Book> removeBook =  new ArrayList<>();
            
           //sai o trung id  
            for (Book b : arrAddBook) {
                out.writeObject("insertBook"); 
                out.writeObject(b);
                resultAdd = (boolean)in.readObject();
                if (resultAdd==false) {
                    JOptionPane.showMessageDialog(this, "Error at BookID : " + b.BookID);
                    removeBook.add(b);
                }
            }
            loadTableAdd(removeBook,1);
            //Cap nhat lai du lieu cho Array arrAddBook
            //Buoc 1 remove arrAddBook
            arrAddBook.removeAll(arrAddBook);
            //Buoc 2 them du lieu cho arrBook tu removeBook
            for (Book b : removeBook) {
                arrAddBook.add(b);
            }
            if (removeBook.isEmpty()) {
                      resetFormAdd();
                       JOptionPane.showMessageDialog(this, "Add Book Successfully ! ");
                       txtQuantity.setEnabled(true);
                       cboCategory.setEnabled(true);
            }
            removeBook.removeAll(removeBook);
        } catch (ClassNotFoundException | IOException ex) {
           System.out.println(ex);
        }
}//GEN-LAST:event_btnCompleteActionPerformed
   
    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCancel1;
    private javax.swing.JButton btnChange;
    private javax.swing.JButton btnComplete;
    private javax.swing.JButton btnDelete1;
    private javax.swing.JButton btnEditCategory;
    private javax.swing.JButton btnInsert;
    private javax.swing.JButton btnNewCategory;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSearchUpdate;
    private javax.swing.JButton btnUpdate1;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JComboBox cbSearchUpdate;
    private javax.swing.JComboBox cboCategory;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lbSumaryAdd;
    private javax.swing.JLabel lblAuthor;
    private javax.swing.JLabel lblBookID;
    private javax.swing.JLabel lblCategory;
    private javax.swing.JLabel lblPublisher;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblpicture;
    private javax.swing.JPanel pnAddBook;
    private javax.swing.JPanel pnChangeBook;
    private javax.swing.JPanel pnMain;
    private javax.swing.JTabbedPane tabChangeBook;
    private javax.swing.JTable tbAddBook;
    private javax.swing.JTable tbUpdate;
    private javax.swing.JTextField txtAuthor;
    private javax.swing.JTextField txtBookID;
    private javax.swing.JTextField txtPublisher;
    private javax.swing.JTextField txtQuantity;
    private javax.swing.JTextField txtSearchUpdate;
    private javax.swing.JTextArea txtSumary;
    private javax.swing.JTextField txtTitle;
    // End of variables declaration//GEN-END:variables
}
